﻿#region 命名空间

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Soft
{
    [Access("DownLoadServer")]
    public partial class Server : WebFactory.PageBase
    {
        private readonly SoftServerManage _myBll = new SoftServerManage();
        private byte ModelId;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModelId = byte.Parse(Request.QueryString["ModelId"]);
            if (!IsPostBack)
            {
                Action = Request["Action"];

                this.GV_List.EmptyDataTemplate = GetTemplate(this);
                BindList();
            }
        }

        #region 当前状态

        private string Action
        {
            set { ViewState["Action"] = value; }
            get
            {
                if (ViewState["Action"] == null)
                {
                    return null;
                }
                return ViewState["Action"].ToString();
            }
        }

        #endregion

        #region 服务器ID

        private int SoftServerID
        {
            set { ViewState["SoftServerID"] = value; }
            get
            {
                if (ViewState["SoftServerID"] == null)
                {
                    return 0;
                }
                return (int) ViewState["SoftServerID"];
            }
        }

        #endregion

        public void BindList()
        {
            this.lblPath.Text = "<a href=\"Server.aspx?ModelId=" + this.ModelId + "\">镜像服务器管理</a>";
            if (Action == null)
            {
                this.Panel1.Visible = true;
                this.Panel2.Visible = false;

                this._myBll.BindList(this.GV_List, this.ModelId);
            }
            else if (Action == "Add")
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                this.txtServerName.Text = "";
                this.txtDownURL.Text = "";
                this.txtServerHTML.Text = "";
                this.ddlServerGroup.Enabled = true;
                this._myBll.BindDDL(this.ddlServerGroup, "添加为服务器组", this.ModelId);
            }
            else if (Action == "Edit")
            {
                this.Panel1.Visible = false;
                this.Panel2.Visible = true;
                this._myBll.BindDDL(this.ddlServerGroup, "添加为服务器组", this.ModelId);
                var model = this._myBll.GetInfo(SoftServerID);
                this.ddlServerGroup.SelectedValue = model.ParentId.ToString();
                this.ddlServerGroup.Enabled = false;
                this.txtServerName.Text = model.ServerName;
                this.txtDownURL.Text = model.DownURL;
                this.txtServerHTML.Text = model.ServerHTML;
            }
        }

        protected void GV_List_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell myTableCell;
                //定义对“删除”单元格的引用
                myTableCell = e.Row.Cells[8];
                //定义对“删除”单元格中控件的引用
                var btnDel = (LinkButton) myTableCell.Controls[0];
                //添加当单击时弹出确认对话框的自定义属性
                btnDel.Attributes.Add("onclick", "return confirm('您确定要删除？');");

                myTableCell = e.Row.Cells[6];
                var lbIsLock = (LinkButton) myTableCell.Controls[0];
                var strTemp = lbIsLock.Text;

                bool flag;

                if (MyValidate.IsNumeric(strTemp))
                {
                    int I = Convert.ToInt16(strTemp);
                    if (I == 1)
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = Convert.ToBoolean(strTemp);
                }

                if (flag)
                {
                    lbIsLock.Text = "锁定";
                    lbIsLock.Attributes.Add("onclick", "return confirm('您确定要解锁？');");
                }
                else
                {
                    lbIsLock.Text = "未锁定";
                    lbIsLock.Attributes.Add("onclick", "return confirm('您确定要锁定？');");
                }
            }
        }


        protected void GV_List_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "IsLock")
            {
                var ID = this.GV_List.DataKeys[index].Value.ToString();
                this._myBll.LockOrOpen(ID, this.UpdatePanel1);
            }

            if (e.CommandName == "EditSoftServer")
            {
                SoftServerID = (int) this.GV_List.DataKeys[index].Value;
                Action = "Edit";
            }
            if (e.CommandName == "DelSoftServer")
            {
                var ID = this.GV_List.DataKeys[index].Value.ToString();
                this._myBll.Delete(ID);
            }
            BindList();
        }


        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Action = null;
            BindList();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Action = "Add";
            BindList();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (this.ddlServerGroup.SelectedValue.Length > 0 && this.txtServerHTML.Text.Trim().Length == 0)
            {
                MsgBox.Message("请输入下载名称", this.UpdatePanel1);
                return;
            }


            var model = new SoftServer();
            if (Action == "Add")
            {
                model.Id = 0;
            }
            else
            {
                model.Id = SoftServerID;
            }
            model.ModelId = this.ModelId;
            model.ParentId = int.Parse(this.ddlServerGroup.SelectedValue);
            model.ServerName = this.txtServerName.Text.Trim();
            model.DownURL = this.txtDownURL.Text.Trim();
            model.ServerHTML = this.txtServerHTML.Text.Trim();

            this._myBll.Add(model);

            Action = null;
            BindList();
        }

        protected string Chang(object serverName, object parentId)
        {
            if (Convert.ToInt32(parentId) != 0)
            {
                return
                    "<IMG src=\"../images/tree/line.gif\" align=absMiddle>&nbsp;&nbsp;&nbsp;&nbsp;<IMG src=\"../images/tree/minusmiddle.gif\" align=absMiddle><IMG src=\"../images/tree/folder.gif\" align=absMiddle>" +
                    serverName;
            }
            return
                "<IMG src=\"../images/tree/plusmiddle.gif\" align=absMiddle><IMG src=\"../images/tree/folder.gif\" align=absMiddle>" +
                serverName;
        }
    }
}